<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2017-11-17
 * Time: 15:51
 * @desc   商城后台 -- 库存管理
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;
class Zbdamage extends BasicAdmin{
	/**
	 * 总部报损管理
	 * @return [type] [description]
	 */
	public function index(){
		$this->title = '成品库存';
		$table = "material_stock_log";		
        $get = $this->request->get();
        $map['a.sign'] = array('eq',3);
        !isset($get['material_type'])?$get['material_type']=1:$get['material_type']=$get['material_type'];
        $map['a.material_type'] = array('eq',$get['material_type']);
        $get['material_type'] == 1?$map['a.material_type']=array('eq',1):$map['a.material_type']=array('eq',2);		//查询条件
        $get['material_type'] == 1?$join=[['ys_goods_material b','a.material_id=b.id','LEFT']]:$join=[['ys_goods b','a.material_id=b.id','LEFT']];		//联查条件
       	$db = Db::name($table)->alias('a')->join($join)->field('a.*,b.name,b.unit')->where($map);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('name', "%{$get[$key]}%");
        }
        if(!isset($get['export'])){
            return parent::_list($db);
        }else{
            $data = $db->select();
            $this->export($data);
        }
        
	}
    /**
     * 门店原材料报损记录 -- 列表
     * @return [type] [description]
     */
    public function smdamage(){
        $this->title = '成品库存';
        $table = "store_material_log";      
        $get = $this->request->get();
        $map['a.sunhao_num'] = array('gt',0);
        $join=[['ys_goods_material b','a.material_id=b.id','LEFT']];      //联查条件
        $db = Db::name($table)->alias('a')->join($join)->field('a.*,b.name,b.unit')->where($map);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('name', "%{$get[$key]}%");
        }
        return parent::_list($db);
    }
    /**
     * 门店商品报损记录 -- 列表
     * @return [type] [description]
     */
    public function sgdamage(){ 
        $this->title = '成品库存';
        $table = "store_goods_log";      
        $get = $this->request->get();
        $map['a.sunhao_num'] = array('gt',0);
        $join=[['ys_goods b','a.goods_id=b.id','LEFT']];      //联查条件
        $db = Db::name($table)->alias('a')->join($join)->field('a.*,b.name,b.unit')->where($map);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('name', "%{$get[$key]}%");
        }
        return parent::_list($db);
    }
    /**
     * 导出报损记录
     * @return [type] [description]
     */
    private function export($list){
        $table = '';
        $table .= "<table>
                        <thead>
                        <tr>
                            <th>".iconv('UTF-8', 'GB2312','名称')."</th>
                            <th>".iconv('UTF-8', 'GB2312','报损数量')."</th>
                            <th>".iconv('UTF-8', 'GB2312','单位')."</th>
                            <th>".iconv('UTF-8', 'GB2312','报损时间')."</th>
                        </tr>
                        </thead>
                    <tbody>";
        foreach ($list as $v){
            $table .= "<tr>
                            <td>".iconv('UTF-8', 'GB2312',$v['name'])."</td>
                            <td>".iconv('UTF-8', 'GB2312',$v['dif_num'])."</td>
                            <td>".iconv('UTF-8', 'GB2312',$v['unit'])."</td>";
            $time = date('Y-m-d H:i',$v['addtime']);
            $table .="<td>".iconv('UTF-8', 'GB2312',$time)."</td>";
        }
        $table .= "</tr></tbody></table>";
        header("Pragma: public");  
        header("Expires: 0");  
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
        header("Content-Type:application/force-download");  
        header("Content-Type:application/vnd.ms-execl");  
        header("Content-Type:application/octet-stream");  
        header("Content-Type:application/download");;  
        header('Content-Disposition:attachment;filename="门店原料申请明细表.xls"');  
        header("Content-Transfer-Encoding:binary"); 
        echo $table;exit();         //必须加上exit();   防止导出调试信息
    }

}
?>